package com.damage.business_dept.service.impl;

import com.damage.business_dept.mapper.IPurchasePlanAuditMapper;
import com.damage.business_dept.service.IPurchasePlanAuditSerivce;
import com.damage.common.config.PurchasePlanConfig;
import com.damage.common.dto.AuditResult;
import com.damage.common.entity.PurchasePlan;
import com.damage.common.exception.IncorrectStateException;
import com.damage.common.service.IPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PurchasePlanAuditServiceImpl implements IPurchasePlanAuditSerivce {

    @Autowired
    IPublicService publicService;

    @Autowired
    IPurchasePlanAuditMapper mapper;

    @Autowired
    PurchasePlanConfig purchasePlanConfig;

    @Override
    @Transactional
    public Integer audit(Integer purchasePlanId, AuditResult auditResult)
            throws IncorrectStateException {
        PurchasePlan purchasePlan = publicService.findPurchasePlanById(purchasePlanId);
        if (purchasePlan.getState() != PurchasePlanConfig.UNAUDITED
                && purchasePlan.getState() != PurchasePlanConfig.AUDITED_NOT_PASS){
            throw new IncorrectStateException("采购计划不是未审核状态或者审核失败状态，不能审核");
        }
        Integer result = mapper.audit(purchasePlanId, auditResult);
        return result;
    }
}
